package com.kuhyygy.activity;

import java.lang.reflect.Field;

import com.kuhyygy.R;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class LinkTextActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.linktestlink);
		String html = "css_easyicon_net_48.png: <img src='css_easyicon_net_48' />"
				+ "easyicon_net_48.png: <img src='easyicon_net_48' />"
				+ "mov_easyicon_net_48.png: <img src='mov_easyicon_net_48' />";
		TextView tw = (TextView) this.findViewById(R.id.linkTextTw);
		tw.setTextSize(14);
		tw.setBackgroundColor(Color.WHITE);
		tw.setTextColor(Color.BLACK);
		
		CharSequence cs = Html.fromHtml(html, new ImageGetter() {
			
			@Override
			public Drawable getDrawable(String source) {
				Drawable d = getResources().getDrawable(getResourceID(source));
				// 必需要设置边界，不然图片不显示
				d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
				return d;
			}
		}, null);
		tw.setText(cs);
		tw.setMovementMethod(LinkMovementMethod.getInstance());
	}

	/**
	 * 通过资源名字获取资源ID
	 * 
	 * @param name
	 * @return
	 */
	private int getResourceID(String name) {
		try {
			Field field = R.drawable.class.getField(name);
			return Integer.parseInt(field.get(null).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

}
